// 导入模块
import Food from './Food'
import ScorePanel from './ScorePanel'
import Snake from './Snake'

// 游戏控制器，控制其他类
class GameControl {
  // 蛇
  snake: Snake;
  // 食物
  food: Food;
  // 记分牌
  scorelPanel: ScorePanel;
  // 点击四个方向
  clickTopEle: HTMLElement;
  clickLeftEle: HTMLElement;
  clickRightEle: HTMLElement;
  clickDownEle: HTMLElement;
  // 存储蛇的移动方向(按键方向)
  direction: string = "";
  directionFlag: string = "";
  // 身体长度
  snakelen =  0
  // 暂停按键
  clickStopGame: HTMLElement;
  // 重启游戏
  resetGame: HTMLElement;
  // 游戏结束标志
  isLive = true;

  constructor() {
    this.snake = new Snake();
    this.food = new Food();
    this.scorelPanel = new ScorePanel(10, 2);
    this.clickTopEle = document.getElementById('btn_top')!;
    this.clickLeftEle = document.getElementById('btn_left')!;
    this.clickRightEle = document.getElementById('btn_right')!;
    this.clickDownEle = document.getElementById('btn_down')!;
    this.clickStopGame = document.getElementById('btn_stop')!;
    this.resetGame = document.getElementById('btn_reset')!;
    this.init();
  }

  // 初始化方法
  init() {
    // 绑定件盘按下
    document.addEventListener('keydown', this.keydownHandler.bind(this))
    // 绑定鼠标点击div
    this.mouseClick();
    // 暂停
    this.clickStopGame.onclick = () => {
      this.stopGame()
    };
    // 重启
    this.resetGame.onclick = () => {
      this.snake.X = 0
      this.snake.Y = 0
      document.getElementById('snakeBody') ? document.getElementById('snake')?.removeChild(document.getElementById('snakeBody')!):null
      // let snakeAll = document.getElementById('snake')!;
      // let snakeBodyAll = snakeAll.getElementsByTagName('div');
      // for (let i = 0; i < snakeBodyAll.length; i++){
      //   snakeAll.removeChild(snakeBodyAll[0]);
      // }
      // this.snake.addBody();
      this.direction = ''
    };
    this.run();
  }

  // 绑定鼠标点击div
  mouseClick() {
    this.clickTopEle.onclick = () => (this.direction = 'ArrowUp', this.directionFlag = 'ArrowUp')
    this.clickLeftEle.onclick = () => (this.direction = 'ArrowLeft', this.directionFlag = 'ArrowLeft')
    this.clickRightEle.onclick = () => (this.direction = 'ArrowRight', this.directionFlag = 'ArrowRight')
    this.clickDownEle.onclick = () => (this.direction = 'ArrowDown', this.directionFlag = 'ArrowDown')
  }

  // 键盘按下事件
  keydownHandler(event: KeyboardEvent) {
    this.direction = event.key
    this.directionFlag = event.key
  }

  // 暂停游戏
  stopGame() {

    if (this.direction != '') {
      this.direction = ''
    } else {
      this.direction = this.directionFlag
    }
  }

  // 控制蛇移动的方法
  run() {

    // 根据方向使蛇位置改变
    let X = this.snake.X;
    let Y = this.snake.Y;

    // 根据按键方向修改XY值
    switch (this.direction) {
      case "ArrowUp":
      case "Up":
        Y -= 10
        break;
      case "ArrowDown":
      case "Down":
        Y += 10
        break;
      case "ArrowLeft":
      case "Left":
        X -= 10
        break;
      case "ArrowRight":
      case "Right":
        X += 10
        break;
    }

    // 检查蛇是否吃到了食物
    this.checkEat(X, Y);

    // 修改X和Y值
    try {
      this.snake.X = X;
      this.snake.Y = Y;
    } catch (e: any) {
      alert(e.message + '游戏结束！');
      this.isLive = false
    }
    // 开启定时调用
    this.isLive && setTimeout(this.run.bind(this), 300 - (this.scorelPanel.level - 1) * 30);
  }

  // 检查蛇是否吃到食物
  checkEat(X: number, Y: number) {
    if (X === this.food.X && Y === this.food.Y) {
      this.food.change();
      this.scorelPanel.addScore();
      this.snake.addBody();
    }
  }
}

export default GameControl;